function getArticleById (article_id, path) {
  var msg = ''
  $.ajaxSettings.async = false
  $.get('/query/',
    {
      'article_id': article_id,
    },
    function (data, status) {
      if (status === 'success') {
        msg = data
      }
    })
  return msg
}

function deleteArticle (article_id, force) {
  axios({
    url: '/delete/',
    method: 'post',
    data: {
      'article_id': article_id,
      'force': force,
    },
    headers: {
      'X-CSRFToken': CsrfToken(),
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }).then(response => {
    location.reload()
  })
}

function comments (article_id, user_id, content) {
  var msg = ''
  $.ajaxSettings.async = false
  $.post('/comments/',
    {
      'article_id': article_id,
      'user_id': user_id,
      'content': content,
    }, function (data, status) {
      msg = [data, status]
    })
  return msg
}

function bcm_get (url, parmas) {
  var msg = ''
  $.ajaxSettings.async = false
  $.get(url, parmas,
    function (data, status) {
      msg = data
    })
  return msg
}

String.prototype.format = function (args) {
  var result = this
  if (arguments.length > 0) {
    if (arguments.length == 1 && typeof (args) == 'object') {
      for (var key in args) {
        if (args[key] != undefined) {
          var reg = new RegExp('({' + key + '})', 'g')
          result = result.replace(reg, args[key])
        }
      }
    } else {
      for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] != undefined) {
          var reg = new RegExp('({)' + i + '(})', 'g')
          result = result.replace(reg, arguments[i])
        }
      }
    }
  }
  return result
}

//点击时间调整底部footer，页面不足时。elem可以是elem、id、class等
function clickElem (elem) {
  $(elem).bind('click', function (e) {
    setTimeout(function () {
      var contentHeight = document.body.scrollHeight
      var winHeight = window.innerHeight

      if (contentHeight < winHeight) {
        $('footer').addClass('fixed-bottom')
      } else {
        $('footer').removeClass('fixed-bottom')
      }
    }, 200)
  })
}

var CsrfToken = function () {
  return $('#csrf_token').attr('csrf_token')
}
